<?php
/**
 * 优惠券管理
 * @author shitou
 *
 */
class CouponAction extends CommonAction{
	
	/**
	 * 添加优惠券
	 */
	public function add(){
		if(IS_AJAX){
			
			$data = sendRest("PAM/Coupon/add",$_POST);
			$this->ajaxReturn($data);
			
		}else{
			
			//所有优惠券类型
			$json = sendRest("PAM/Coupon/type");
			$this->assign("couponType",$json['data']);
			
			//所有获取优惠券的场景
			$data = sendRest("PAM/Coupon/sence");
			$this->assign("couponSence",$data['data']);
			
			//获取所有商品
			$product = sendRest("PMS/Product/index");
			$this->assign("proList",$product['data']['list']);
			
			//获取所有商品类别
			$category = sendRest("PMS/Column/view");
			$this->assign("cateList",$category['data']);
			
			//获取商品品牌
			$brand = sendRest("PMS/Brand/view");
			$this->assign("barndList",$brand['data']);
			
			//获取会员等级
			$level = sendRest("MMS/Level/index");
			$this->assign("levelList",$level['data']);
			
			//获取会员分组
			$group = sendRest("MMS/Group/index");
			$this->assign("groupList",$group['data']);
			
			$this->display();
		}
	}
	
	public function type(){
		
		$type = $this->_param('val','trim','');
		$id   = $this->_param('id','intval',0);
		if($id){
			
			$map['id'] = $this->_param("id","intval",0);
			$data = sendRest("PAM/Coupon/detail",$map);

			$this->assign("detail",$data['data']);
			$attr = json_decode($data['attr'],true);
		}else{
			$attr = '';
		}
		
		//获取所有商品
		$product = sendRest("PMS/Product/index");
		$this->assign("proList",$product['data']['list']);
		
		$this->assign("type",$type);
		$this->assign("attr",$attr);
		$this->display();
	}
	
	/**
	 * 修改优惠券
	 */
	public function edit(){
		if(IS_AJAX){
				
			$data = sendRest("PAM/Coupon/edit",$_POST);
			$this->ajaxReturn($data);
				
		}else{
			
			$map['id'] = $this->_param("id","intval",0);
			
			//获取商品详情	
			$data = sendRest("PAM/Coupon/detail",$map);
			$this->assign("detail",$data['data']);
			
			//所有优惠券类型
			$json = sendRest("PAM/Coupon/type");
			$this->assign("couponType",$json['data']);
				
			//所有获取优惠券的场景
			$view = sendRest("PAM/Coupon/sence");
			$this->assign("couponSence",$view['data']);
				
			//获取所有商品
			$product = sendRest("PMS/Product/index");
			$this->assign("proList",$product['data']['list']);
				
			//获取所有商品类别
			$category = sendRest("PMS/Column/view");
			$this->assign("cateList",$category['data']);
				
			//获取商品品牌
			$brand = sendRest("PMS/Brand/view");
			$this->assign("barndList",$brand['data']);
				
			//获取会员等级
			$level = sendRest("MMS/Level/index");
			$this->assign("levelList",$level['data']);
				
			//获取会员分组
			$group = sendRest("MMS/Group/index");
			$this->assign("groupList",$group['data']);
			
			//优惠规则
			$query = json_decode($data['data']['config'],true);
			$this->assign("config",$query);
			
			$this->display();
		}
	}
	
	/**
	 * 删除优惠券
	 */
	public function del(){
		
		$map['val'] = $this->_param("val","trim","");
		
		$data = sendRest("PAM/Coupon/del",$map);
		$this->ajaxReturn($data);
	}
	
	/**
	 * 优惠券列表
	 */
	public function index(){
		
		if(IS_AJAX){
			
			$desc    = $this->_get("orderDirection","trim",   "desc");;
			$order   = $this->_get("orderName",     "trim",   "addtime");
			$pageNum = $this->_get("pageNum",       "intval",  C('PAGE_NUMS'));
			$nowPage = $this->_get("p",             "intval",  1);
			$where   = $_POST['where'];
			$cateid  = $this->_param("cateid","intval",0);
			
			$map   = array("where"=>$where,"cateid"=>$cateid,"pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
				
			$json  = sendRest("PAM/Coupon/index",$map);
			$type  = sendRest("PAM/Coupon/type");
			$data  = $json['data'];
			$count = $data['count'];
			
			$totalPage = ceil($count/$pageNum);
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
			
			foreach($data['list'] AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'title'    => $row['title'],
						'total'    => $row['total'],
						'prefix'   => $row['prefix'],
						'type'     => $type['data'][$row['type']],
						'begin'    => $row['begin'],
						'end'      => $row['end'],
						'isshow'   => $row['isshow']==1?"是":"否",
						'addtime'  => date('Y-m-d',$row['addtime']),
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,"JSON");
			
		}else{
			$this->display();
		}
	}
}

?>